package com.joymart.admin.rest;


import com.joymart.common.model.Range;
import com.joymart.common.utils.DatetimeUtils;
import com.joymart.common.utils.SecurityUtils;
import com.joymart.order.model.Order;
import com.joymart.order.service.OrderEventService;
import com.joymart.order.service.OrderFlowService;
import com.joymart.order.service.OrderQueryService;
import com.joymart.order.service.OrderService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/admin")
public class OrderAdminController {

    @Autowired
    private OrderQueryService orderQueryService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderFlowService orderFlowService;
    @Autowired
    private OrderEventService orderEventService;

    @GetMapping("/orders")
    public Page<OrderQueryService.OrderAdminDTO> searchOrders(
            LocalDateTime start,
            LocalDateTime end,
            @RequestParam(value = "pageNo", defaultValue = "0")Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "20")Integer pageSize,
            @RequestParam(value = "orderId", defaultValue = "")String orderId,
            @RequestParam(value = "receiver", defaultValue = "")String receiver,
            @RequestParam(value = "category", defaultValue = "") String category
    ){
        Range range = Range.of(start, end);
        DatetimeUtils.checkRangeWithin(90,range);


        if(StringUtils.hasText(category)){
            return orderQueryService.searchAdminOrders(range, orderId, receiver, PageRequest.of(pageNo, pageSize),
                    Order.Status.valueOf(category));
        }else{
            return orderQueryService.searchAdminOrders(range, orderId, receiver, PageRequest.of(pageNo, pageSize));
        }

    }


    /**
     * 确认管理店铺付款
     * @param orderId
     * @return
     */
    @PutMapping(value = "/orders/{id}", params = "action=CONFIRM_SECOND_SELLER_PAID")
    public OrderQueryService.OrderDTO confirmBuyerPaid(@PathVariable("id") String orderId) {
        SecurityUtils.requiresRestAuthority("OrderRelated");
        Order order = orderService.getById(orderId);
        orderFlowService.confirmSecondSellerPaid(order);
        return orderQueryService.findById(orderId);
    }
    /**
     * 发货
     * @param orderId
     * @return
     */
    @PutMapping(value = "/orders/{id}", params = "action=MAKE_DELIVERY")
    public OrderQueryService.OrderDTO makeDelivery(@PathVariable("id") String orderId,
        @RequestBody  DeliveryParam deliveryParam) {
        SecurityUtils.requiresRestAuthority("OrderRelated");
        Order order = orderService.getById(orderId);
        order.setDeliveryVendorId(deliveryParam.getDeliveryVendorId());
        order.setDeliveryNo(deliveryParam.getDeliveryNo());
        orderFlowService.makeDelivery(order);
        return orderQueryService.findById(orderId);
    }

    @Data
    private static class DeliveryParam{
        // 快递公司代号
        private String deliveryVendorId;
        // 快递单号
        private String deliveryNo;
    }



    /**
     * 订单时间线
     * @param orderId
     * @return
     */
    @GetMapping(value = "/orders/{id}/timeline")
    public Object timeline(@PathVariable("id") String orderId) {
        return orderEventService.organizeTimeline(orderId);
    }
}
